<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js">/*
 * Copyright (c) 2016 VUI(https://git.oschina.net/durcframework/vui) All rights reserved.
 */
 
/*! LAB.js (LABjs :: Loading And Blocking JavaScript)
    v2.0.3 (c) Kyle Simpson
    MIT License
*/
(function(o){var K=o.$LAB,y=&quot;UseLocalXHR&quot;,z=&quot;AlwaysPreserveOrder&quot;,u=&quot;AllowDuplicates&quot;,A=&quot;CacheBust&quot;,B=&quot;BasePath&quot;,C=/^[^?#]*\//.exec(location.href)[0],D=/^\w+\:\/\/\/?[^\/]+/.exec(C)[0],i=document.head||document.getElementsByTagName(&quot;head&quot;),L=(o.opera&amp;&amp;Object.prototype.toString.call(o.opera)==&quot;[object Opera]&quot;)||(&quot;MozAppearance&quot;in document.documentElement.style),q=document.createElement(&quot;script&quot;),E=typeof q.preload==&quot;boolean&quot;,r=E||(q.readyState&amp;&amp;q.readyState==&quot;uninitialized&quot;),F=!r&amp;&amp;q.async===true,M=!r&amp;&amp;!F&amp;&amp;!L;function G(a){return Object.prototype.toString.call(a)==&quot;[object Function]&quot;}function H(a){return Object.prototype.toString.call(a)==&quot;[object Array]&quot;}function N(a,c){var b=/^\w+\:\/\//;if(/^\/\/\/?/.test(a)){a=location.protocol+a}else if(!b.test(a)&amp;&amp;a.charAt(0)!=&quot;/&quot;){a=(c||&quot;&quot;)+a}return b.test(a)?a:((a.charAt(0)==&quot;/&quot;?D:C)+a)}function s(a,c){for(var b in a){if(a.hasOwnProperty(b)){c[b]=a[b]}}return c}function O(a){var c=false;for(var b=0;b&lt;a.scripts.length;b++){if(a.scripts[b].ready&amp;&amp;a.scripts[b].exec_trigger){c=true;a.scripts[b].exec_trigger();a.scripts[b].exec_trigger=null}}return c}function t(a,c,b,d){a.onload=a.onreadystatechange=function(){if((a.readyState&amp;&amp;a.readyState!=&quot;complete&quot;&amp;&amp;a.readyState!=&quot;loaded&quot;)||c[b])return;a.onload=a.onreadystatechange=null;d()}}function I(a){a.ready=a.finished=true;for(var c=0;c&lt;a.finished_listeners.length;c++){a.finished_listeners[c]()}a.ready_listeners=[];a.finished_listeners=[]}function P(d,f,e,g,h){setTimeout(function(){var a,c=f.real_src,b;if(&quot;item&quot;in i){if(!i[0]){setTimeout(arguments.callee,25);return}i=i[0]}a=document.createElement(&quot;script&quot;);if(f.type)a.type=f.type;if(f.charset)a.charset=f.charset;if(h){if(r){e.elem=a;if(E){a.preload=true;a.onpreload=g}else{a.onreadystatechange=function(){if(a.readyState==&quot;loaded&quot;)g()}}a.src=c}else if(h&amp;&amp;c.indexOf(D)==0&amp;&amp;d[y]){b=new XMLHttpRequest();b.onreadystatechange=function(){if(b.readyState==4){b.onreadystatechange=function(){};e.text=b.responseText+&quot;\n//@ sourceURL=&quot;+c;g()}};b.open(&quot;GET&quot;,c);b.send()}else{a.type=&quot;text/cache-script&quot;;t(a,e,&quot;ready&quot;,function(){i.removeChild(a);g()});a.src=c;i.insertBefore(a,i.firstChild)}}else if(F){a.async=false;t(a,e,&quot;finished&quot;,g);a.src=c;i.insertBefore(a,i.firstChild)}else{t(a,e,&quot;finished&quot;,g);a.src=c;i.insertBefore(a,i.firstChild)}},0)}function J(){var l={},Q=r||M,n=[],p={},m;l[y]=true;l[z]=false;l[u]=false;l[A]=false;l[B]=&quot;&quot;;function R(a,c,b){var d;function f(){if(d!=null){d=null;I(b)}}if(p[c.src].finished)return;if(!a[u])p[c.src].finished=true;d=b.elem||document.createElement(&quot;script&quot;);if(c.type)d.type=c.type;if(c.charset)d.charset=c.charset;t(d,b,&quot;finished&quot;,f);if(b.elem){b.elem=null}else if(b.text){d.onload=d.onreadystatechange=null;d.text=b.text}else{d.src=c.real_src}i.insertBefore(d,i.firstChild);if(b.text){f()}}function S(c,b,d,f){var e,g,h=function(){b.ready_cb(b,function(){R(c,b,e)})},j=function(){b.finished_cb(b,d)};b.src=N(b.src,c[B]);b.real_src=b.src+(c[A]?((/\?.*$/.test(b.src)?&quot;&amp;_&quot;:&quot;?_&quot;)+~~(Math.random()*1E9)+&quot;=&quot;):&quot;&quot;);if(!p[b.src])p[b.src]={items:[],finished:false};g=p[b.src].items;if(c[u]||g.length==0){e=g[g.length]={ready:false,finished:false,ready_listeners:[h],finished_listeners:[j]};P(c,b,e,((f)?function(){e.ready=true;for(var a=0;a&lt;e.ready_listeners.length;a++){e.ready_listeners[a]()}e.ready_listeners=[]}:function(){I(e)}),f)}else{e=g[0];if(e.finished){j()}else{e.finished_listeners.push(j)}}}function v(){var e,g=s(l,{}),h=[],j=0,w=false,k;function T(a,c){a.ready=true;a.exec_trigger=c;x()}function U(a,c){a.ready=a.finished=true;a.exec_trigger=null;for(var b=0;b&lt;c.scripts.length;b++){if(!c.scripts[b].finished)return}c.finished=true;x()}function x(){while(j&lt;h.length){if(G(h[j])){try{h[j++]()}catch(err){}continue}else if(!h[j].finished){if(O(h[j]))continue;break}j++}if(j==h.length){w=false;k=false}}function V(){if(!k||!k.scripts){h.push(k={scripts:[],finished:true})}}e={script:function(){for(var f=0;f&lt;arguments.length;f++){(function(a,c){var b;if(!H(a)){c=[a]}for(var d=0;d&lt;c.length;d++){V();a=c[d];if(G(a))a=a();if(!a)continue;if(H(a)){b=[].slice.call(a);b.unshift(d,1);[].splice.apply(c,b);d--;continue}if(typeof a==&quot;string&quot;)a={src:a};a=s(a,{ready:false,ready_cb:T,finished:false,finished_cb:U});k.finished=false;k.scripts.push(a);S(g,a,k,(Q&amp;&amp;w));w=true;if(g[z])e.wait()}})(arguments[f],arguments[f])}return e},wait:function(){if(arguments.length&gt;0){for(var a=0;a&lt;arguments.length;a++){h.push(arguments[a])}k=h[h.length-1]}else k=false;x();return e}};return{script:e.script,wait:e.wait,setOptions:function(a){s(a,g);return e}}}m={setGlobalDefaults:function(a){s(a,l);return m},setOptions:function(){return v().setOptions.apply(null,arguments)},script:function(){return v().script.apply(null,arguments)},wait:function(){return v().wait.apply(null,arguments)},queueScript:function(){n[n.length]={type:&quot;script&quot;,args:[].slice.call(arguments)};return m},queueWait:function(){n[n.length]={type:&quot;wait&quot;,args:[].slice.call(arguments)};return m},runQueue:function(){var a=m,c=n.length,b=c,d;for(;--b&gt;=0;){d=n.shift();a=a[d.type].apply(null,d.args)}return a},noConflict:function(){o.$LAB=K;return m},sandbox:function(){return J()}};return m}o.$LAB=J();(function(a,c,b){if(document.readyState==null&amp;&amp;document[a]){document.readyState=&quot;loading&quot;;document[a](c,b=function(){document.removeEventListener(c,b,false);document.readyState=&quot;complete&quot;},false)}})(&quot;addEventListener&quot;,&quot;DOMContentLoaded&quot;)})(this);

;(function(){
	
// JS目录
// 存放界面库的根目录,这里向外部提供一个AJUI_ROOT变量
var dir = window.AJUI_DIR || '/'
var jsMapPath = 'loader/jsmap.js'
var isMapFileLoaded = false;
// 给已经加载过的js做个标记
var js_store = {};
var jsVersion = new Date().getTime();

var allwaysLoad = ['Msg']; // 始终加载

// 是否需要导入
<span id='global-method-isNeedLoad'>/** @private */
</span>function isNeedLoad(moduleName) {
	if(!js_store[moduleName]){
		js_store[moduleName] = true;
		return true;
	}else{
		return false;
	}
}

function buildJsPath(jsMap) {
	var jsPath = [];
	
	appendJsPath(jsPath,jsMap);
	
	return jsPath;
}

function appendJsPath(jsPath,jsMap) {
	for(var moduleName in jsMap) {
		doAppendJsPath(jsPath,moduleName,jsMap);
	}
}

function doAppendJsPath(jsPath,moduleName,jsMap) {
	if(isNeedLoad(moduleName)) {
		var jsInfo = jsMap[moduleName];
		if(!jsInfo) {
			alert('没有找到名为[' + moduleName + ']的JS文件,dir:' + dir)
			throw new Error('没有找到名为[' + moduleName + ']的JS文件,dir:' + dir);
		}
		
		// 获取其依赖的JS
		var requireJS = jsMap[moduleName].require || [];
		// 先导入依赖的JS
		for(var i=0,len=requireJS.length; i&lt;len; i++) {
			var moduleName = requireJS[i];
			doAppendJsPath(jsPath,moduleName,jsMap);
		}
		
		var src = getJsSrc(moduleName,jsInfo);
		
		jsPath.push(src);
	}	
}

// 获取JS的路径
function getJsSrc(moduleName,jsInfo) {
	var path = jsInfo.path;
	
	if(!path) {
		throw new Error('没有找到[' + moduleName + ']JS文件路径,dir:' + dir);
	}
	
	//return dir + path;
	return dir + path + '?v=' + jsVersion++;
}

function loadJsMapFile(callback) {
	if(!isMapFileLoaded) {
		isMapFileLoaded = true;
		var jsPath = dir + jsMapPath;
		doLoad(jsPath,callback);
	}else{
		callback();
	}
}

function loadCommonJs(callback) {
	var jsMap = VUI_JsMap.common;
	var jsPath = buildJsPath(jsMap);
	doLoad(jsPath,callback);
}

function loadModuleJs(moduleNames,callback) {
	var jsMap = VUI_JsMap.module;
	var jsPath = [];
	
	if(typeof moduleNames === 'string') {
		moduleNames = [moduleNames]
	}
	
	moduleNames = moduleNames.concat(allwaysLoad);
	
	for(var i=0, len=moduleNames.length;i&lt;len; i++) {
		doAppendJsPath(jsPath,moduleNames[i],jsMap);
	}
	
	doLoad(jsPath,callback);
}

function doLoad(jsPath,callback) {
	if(jsPath &amp;&amp; jsPath.length &gt; 0) {
		$LAB
			.script(jsPath)
			.wait(function(){ // 所有JS加载完毕后触发
				try{
					callback &amp;&amp; callback();
				}catch(e) {
					console.error(e);
				}
			});
	}else{
		callback &amp;&amp; callback();
	}
	
}

<span id='global-method-extend'>/**
</span> * 拷贝数据,c拷贝到o
 * @ignore
 * @param o 原数据
 * @param c 拷贝的数据
 * @return 返回o
 */
function extend(o, c) {		 
    if(o &amp;&amp; c &amp;&amp; typeof c == 'object') {
        for(var p in c) {
            o[p] = c[p];
        }
    }
    return o;
}

<span id='VUI'>/**
</span> * @class VUI
 */
var JsLoader = {
<span id='VUI-method-use'>	/**
</span>	 * 加载js
	 * @param {String} jsNames 可以是字符串,也可以是字符串数组
	 * @param {Function} successFun JS加载完毕后的回调函数
	 */
	use:function(jsNames,successFun) {
		loadJsMapFile(function(){
			loadCommonJs(function(){
				loadModuleJs(jsNames,function(){
					successFun &amp;&amp; successFun();
				});
			});
		});
		
		return this;
	}
<span id='VUI-method-dir'>	/**
</span>	 * 设置ajui存放的根目录
	 * @param {String} root
	 */
	,dir:function(root) {
		dir = root;
		return this;
	}
}

window.VUI = extend(window.VUI || {},JsLoader);

})();</pre>
</body>
</html>
